#!/bin/sh
# This file run at 00:00
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
ROOT_DIR=`cd $(dirname $0)/..; pwd`

# stop logger
EXEFILE=`which varnishncsa`
PIDFILE=$ROOT_DIR/bin/varnishncsa.pid
if [ -e $PIDFILE ]; then
	PID=`cat $PIDFILE` 
	if [ /proc/$PID/exe -ef $EXEFILE ]; then
		echo 'kill varnishncsa'
		kill $PID
	fi
fi

# backup log and remove older
date=$(date -d "yesterday" +"%Y-%m-%d")
mv $ROOT_DIR/log/varnish.log $ROOT_DIR/log/${date}.log
gzip -c $ROOT_DIR/log/${date}.log  >$ROOT_DIR/log/${date}.log.gz
rm -f $ROOT_DIR/log/${date}.log
find $ROOT_DIR/log/ -name "*.log" -mtime +30 -delete


# start logger
varnishncsa  \
-n $ROOT_DIR/bin \
-P $ROOT_DIR/bin/varnishncsa.pid \
-w $ROOT_DIR/log/varnish.log &
